import CommonConstant from "../constant/CommonConstant";
import BasePanel from "../game/Panel/BasePanel";
import EnumData from "../Enum/EnumData";
import LoadingPanel from "../game/Panel/LoadingPanel";
import GamePanel from "../game/Panel/GamePanel";
import MainUIPanel from "../game/Panel/MainUIPanel";
import { LevelData } from "../game/Data/LevelData";
import DialogPanel from "../game/Panel/DialogPanel";
import PropMessagePanel from "../game/Panel/PropMessagePanel";
import CompositePanel from "../game/Panel/CompositePanel";
import GamePropObjPrefabRoot from "../game/Prefabs/InteractivObj/Common/GamePropObjPrefabRoot";
import CompletePanel from "../game/Panel/CompletePanel";
import SelectLevelPanel from "../game/Panel/SelectLevelPanel";
import HintPanel from "../game/Panel/HintPanel";

export default class SceneManager {
    /** Fui*/
    private FUILayer: Laya.Sprite;
    /** 面板 */
    private PanelLayer: Laya.Sprite;
    
    private PanelDic: Map<EnumData.EnumPanelType,BasePanel>;

    /** 是否禁止免转按钮*/
    public bProhibitWalletBtn: boolean = false; //有弹窗时禁止免转按钮

    //游戲場景
    private m_scene3D:Laya.Scene3D;

    //交互物體prefab場景
    public GamePropObjScene:Laya.Scene3D;

    public GamePropObjPrefabRoot:GamePropObjPrefabRoot;

    public init(): void {
        this.PanelDic = new Map();
        this.FUILayer = new Laya.Sprite();
        this.PanelLayer = new Laya.Sprite();
        Laya.stage.addChild(this.PanelLayer);
        Laya.stage.addChild(this.FUILayer);
        this.FUILayer.addChild(fairygui.GRoot.inst.displayObject);
    }

    public AddFUI(fui: fairygui.GComponent) {
        fairygui.GRoot.inst.addChild(fui);
        // fairygui.GRoot.inst.setScale
    }

    public RemoveFUI(fui: fairygui.GComponent) {
        fui.removeFromParent();
        fui.dispose();
    }

    public get CurrentScene3D():Laya.Scene3D{
        return this.m_scene3D;
    }

    private GetPanel(id: EnumData.EnumPanelType, data: any = null) {
        var panel: BasePanel = null;
        if (this.PanelDic.has(id)) {
            panel = this.PanelDic.get(id);
        }
        else {
            switch (id) {
                case EnumData.EnumPanelType.LoadingPanel:
                    panel = new LoadingPanel(id);
                    break;
                case EnumData.EnumPanelType.MainUIPanel:
                    panel = new MainUIPanel(id);
                    break;
                case EnumData.EnumPanelType.GamePanel:
                    panel = new GamePanel(id);
                    break;
                case EnumData.EnumPanelType.PropMessagePanel:
                    panel = new PropMessagePanel(id,data);
                    break;
                case EnumData.EnumPanelType.CompositePanel:
                    panel = new CompositePanel(id,data);
                        break;
                case EnumData.EnumPanelType.CompletePanel:
                    panel = new CompletePanel(id);
                        break;
                case EnumData.EnumPanelType.SelectLevelPanel:
                    panel = new SelectLevelPanel(id);
                    break;
                case EnumData.EnumPanelType.HintPanel:
                    panel = new HintPanel(id);
                    break;
                default:
                    break;
            }
            this.PanelDic.set(id, panel);
        }

        return panel;
    }

    /**
     * 1.这里panel的添加到PanelLayer层暂时是没有显示的，显示的ui是panel内部的fui
     * 2.new一个panel的同时，在panel内部new fui并添加在fui层
     * */
    public Create(id: EnumData.EnumPanelType, data: any = null) {
        var panel: BasePanel = this.GetPanel(id, data) as BasePanel;
        this.PanelLayer.addChild(panel);
        if (!panel) return;
        return panel;
    }

    public close(id: EnumData.EnumPanelType, bRemove: boolean = true) {
        if (!this.PanelDic.has(id)) return null;
        var panel: BasePanel = this.GetPanel(id) as BasePanel;
        if (!panel) return;
        panel.Close(bRemove, id);
    }

    public destroy(id: EnumData.EnumPanelType) {
        if (!this.PanelDic.has(id)) return null;
        this.bProhibitWalletBtn = false;
        var panel: BasePanel = this.GetPanel(id) as BasePanel;
        this.PanelDic.delete(id);
        if (!panel) return;
        panel.removeSelf();
        panel.destroy(true);
    }

    public GetPanelById(id: EnumData.EnumPanelType) {
        if (this.PanelDic.has(id)) {
            var panel: BasePanel = this.PanelDic.get(id);
            if (!panel) return;
            return panel;
        }
        return null;
    }

    /**
     * 清除掉所有的panel
     */
    public clearAllPanel(): void {
        window.gameFacade.TipMng.close();
        this.close(EnumData.EnumPanelType.CompositePanel);
        this.close(EnumData.EnumPanelType.PropMessagePanel);
        this.close(EnumData.EnumPanelType.CompletePanel);
        this.close(EnumData.EnumPanelType.SelectLevelPanel);
        this.close(EnumData.EnumPanelType.HintPanel);
    }

    public EnterGameScene(levelData:LevelData) {
        this.clearAllPanel();
        let self = this;
        this.m_scene3D = null;
        //清理上一个场景的ui、缓存
        var id = EnumData.EnumPanelType.MainUIPanel;
        if (this.PanelDic.has(id)) {
            var panel = this.PanelDic.get(id) as MainUIPanel;
            panel.ExitMainScene();
            this.close(id);
        }

        var scenePath = CommonConstant.GetSceneNameByLevelData(levelData);
  
        let gameFuiArr = [
        { url: CommonConstant._fuiGamePropAtlasPath.image, type: Laya.Loader.IMAGE },
        { url: CommonConstant._fuiGamePropAtlasPath.fui, type: Laya.Loader.BUFFER },
        { url: CommonConstant._fuiGameUIPanelPath.image, type: Laya.Loader.IMAGE },
        { url: CommonConstant._fuiGameUIPanelPath.fui, type: Laya.Loader.BUFFER },
        { url: CommonConstant._fuiHintPanelPath.image, type: Laya.Loader.IMAGE },
        ];

        console.log("scenePath:"+scenePath);
        window.gameFacade.ResourceMng.LoadNewScene(gameFuiArr, scenePath,null, Loaded);
        function Loaded(scene: Laya.Scene3D) {
            var panel = window.gameFacade.SceneMng.Create(EnumData.EnumPanelType.GamePanel) as GamePanel;
            self.m_scene3D  = scene;
            self.m_scene3D.addChild(self.GamePropObjPrefabRoot.owner);
            panel.EnterAfter(scene,levelData);
        }
    }

    //进入主界面场景
    public EnterMainScene(callBack: Function = null) {
        let self = this;
        this.clearAllPanel();
        var id = EnumData.EnumPanelType.GamePanel;
        if (this.PanelDic.has(id)) {
            var panel = this.PanelDic.get(id) as GamePanel;
       
            panel.ExitGameScene();
            
            this.close(id);
        }
        let mainUIFuiArr = [
            { url: CommonConstant._fuiMainUIPanelPath.image, type: Laya.Loader.IMAGE },
            { url: CommonConstant._fuiSelectLevelPanelPath.image, type: Laya.Loader.IMAGE },
            { url: CommonConstant._fuiMainUIPanelPath.fui, type: Laya.Loader.BUFFER },
        ];

        window.gameFacade.ResourceMng.LoadNewScene(mainUIFuiArr, null, CommonConstant._allGamePropObj, Loaded);
        function Loaded(gamePropObjPrefabRoot: GamePropObjPrefabRoot) {
            var panel = window.gameFacade.SceneMng.Create(EnumData.EnumPanelType.MainUIPanel) as MainUIPanel;
            panel.EnterMainScene();
            self.GamePropObjPrefabRoot = gamePropObjPrefabRoot;
            if(self.GamePropObjScene == null){
                self.GamePropObjScene = new Laya.Scene3D();
                Laya.stage.addChild(self.GamePropObjScene);
            }
            Laya.Resource.destroyUnusedResources();
            if(callBack != null){
                callBack();
            }
        }
    }
}

